/*******************************************
The URL object encapsulates the state needed
to manage a connection to a remote resource.
*/

var URL = function () {

	this.__xhr = new XMLHttpRequest();

	/*
	This attribute allows you to control whether a URL object will follow redirects
	automatically. Setting this to false will allow you to get the 302
	redirect response and process it as you wish.
	*/
	this.autoRedirect = true;
	
};

URL.prototype.addPostFile = function (filename, path) {
	if (!this.__postFiles)
		this.__postFiles = new Array();
	 
	this.__postFiles.push({'filename':filename, 'path':path});
}

URL.prototype.cancel = function () {
	this.__xhr.abort();
}

URL.prototype.clear = function () {
	delete this.__postFiles;
	this.autoRedirect = false;
	this.location = '';
	this.outputFile = '';
	this.postData = '';
}

URL.prototype.fetch = function (location) {
	this.location = location;
	this.__fetch(false);
}

URL.prototype.fetchAsync = function (callback) {
	this.__callback = callback;
	this.__fetch(true);
}

URL.prototype.__getHttpMethod = function () [
	return this.postData || (this.__postFiles && this.__postFiles.length) ? 'POST' : 'GET';
}

URL.prototype.__readystatechanged = function () {
    if (this.__xhr.readyState != 4)
		return;

	this.result = 200 != this.response = this.__xhr.status ? this.__xhr.statusText : this.__xhr.responseText;
	this.responseData = this.__xhr.responseText;
	
	/*
	if (outputFile)
		widget.system(perform file writing here);
	*/
	
	if ('function' == typeof(this.__callback))
		this.__callback(this);
}

URL.prototype.__fetch = function (async) {
	if (!this.location)
		throw new Error('URL location not specified.');
		
	this.__xhr.open(this.__getHttpMethod(), this.location, async);

	///TODO: Use widget.system for reading file content and 
	//then append it to 'content' variable follow HTTP protocol rules.
	
	var content = this.postData;
	
	this.__xhr.onreadystatechange = this.__readystatechanged.delegate(this);
	this.__xhr.send(content);
	
	if (!async)
		return this.result;
}

URL.prototype.getResponseHeaders = function (name) {
	///TODO: Initialize array from returned string.
	
	if (name == '*')
		return this.__xhr.getAllResponseHeaders();
		
	return this.__xhr.getResponseHeader(name);
}

URL.prototype.setRequestHeader = function (name, value) {
	this.__xhr.setRequestHeader(name, value);
}

/*******************
Animators and Animations
*/

if (!AppleAnimator || !AppleAnimation)
	throw new Error('Can not find AppleAnimator class.');





/*******************
Widget global object
*/

var Widget = { DASHBOARD : true };

Widget.print = function (message) {
	///TODO: perform debugging messages writing
}

Widget.savePreferences = function () {
	for (key in preferences)
		if (undefined !== preferences[key].value)
			widget.setPreferenceForKey(preferences[key].value, key);
}

Widget.loadPreferences = function () {
	for (key in preferences)
		if (undefined !== preferences[key].value)
			preferences[key].value = widget.preferenceForKey(key);
}

Widget.openURL = widget.openURL;
Widget.prepareForTransition = widget.prepareForTransition;
Widget.performTransition = widget.performTransition;

Widget.updateNow = function () {};